<template>
    <div class="app-container">
        <el-row>
            <el-col :span="24" class="card-box">
                <el-card>
                       
                            <el-button
                            type="primary"
                            plain
                            icon="Plus"
                            @click="handleUpdateOrderStatus"
                            v-hasPermi="['medicine:order:edit']"
                            >重新提交审核</el-button>
                       
                      
                            <el-button
                            type="primary"
                            plain
                            icon="Plus"
                            @click="handleBack"
                            >返回</el-button>
                       
                </el-card>
            </el-col>
            <el-col :span="24" class="card-box">
                <el-card>
                    <template #header>
                        <Monitor style="width: 1em; height: 1em; vertical-align: middle;" /> <span
                            style="vertical-align: middle;">采购单信息</span>
                    </template>
                    <div class="el-table el-table--enable-row-hover el-table--medium">
                        <el-form :model="orderForm" ref="queryRef"  v-show="showSearch"
                            label-width="68px">
                            <el-form-item label="订单ID" prop="orderId">
                                <el-input v-model="orderForm.orderId" disabled placeholder="请输入订单ID" clearable
                                     />
                            </el-form-item>
                            <el-form-item label="订单编号" prop="orderNum">
                                <el-input disabled v-model="orderForm.orderNum" placeholder="请输入订单编号" clearable
                                    />
                            </el-form-item>
                        
                            <el-form-item label="供应商" prop="orderSupplier">
                                <el-select v-model="orderForm.supplierId" placeholder="请选择供应商" style="width: 200px;">
                                    <el-option v-for="dict in supplier_info" :key="dict" :label="dict.supplierName"
                                        :value="dict.supplierId"></el-option>
                                </el-select>
                            </el-form-item>
                            <el-form-item label="备注" prop="remark">
                                <el-input v-model="orderForm.remark" type="textarea" placeholder="请输入内容" />
                            </el-form-item>
                            <el-form-item>
                                <el-button type="primary" icon="Search" @click="handleUpdateOrder">确定更新</el-button>
                            </el-form-item>
                        </el-form>
                    </div>
                </el-card>
            </el-col>
            <el-col :span="24" class="card-box">
                <el-card>
                    <template #header>
                        <Monitor style="width: 1em; height: 1em; vertical-align: middle;" /> <span
                            style="vertical-align: middle;">采购单详情</span>
                    </template>
                    <div class="el-table el-table--enable-row-hover el-table--medium">
                        <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch"
                            label-width="68px">
                            <el-form-item label="批次号" prop="detailBatch">
                                <el-input v-model="queryParams.detailBatch" placeholder="请输入批次号" clearable
                                    />
                            </el-form-item>
                            <el-form-item label="药品名称" prop="drugName">
                                <el-input v-model="queryParams.drugName" placeholder="请输入药品名称" clearable
                                     />
                            </el-form-item>
                            <el-form-item>
                                <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                                <el-button icon="Refresh" @click="resetQuery">重置</el-button>
                            </el-form-item>
                        </el-form>

                        <el-row :gutter="10" class="mb8">
                            <el-col :span="1.5">
                                <el-button type="warning" plain icon="Download" @click="handleExport"
                                    v-hasPermi="['medicine:detail:export']">导出</el-button>
                            </el-col>
                            <el-col :span="1.5">
                                <el-button
                                type="danger"
                                plain
                                icon="Delete"
                                :disabled="multiple"
                                @click="handleDelete"
                                v-hasPermi="['medicine:orderDetail:remove']"
                                >删除</el-button>
                            </el-col>
                            <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
                        </el-row>

                        <el-table v-loading="loading" :data="detailList" @selection-change="handleSelectionChange">
                            <el-table-column type="selection" width="55" align="center" />
                            <el-table-column label="详情ID" align="center" prop="detailId" />
                            <el-table-column label="药品名称" align="center" prop="hisMedicineDrug.drugName" />
                            <el-table-column label="采购数量" align="center" prop="detailNum">
                                <template #default="scope">
                                    <el-input-number v-model="scope.row.detailNum" :min="0" size="small" @change="updateNum(scope.row)"/>    
                                </template>
                            </el-table-column>
                            <el-table-column label="订单ID" align="center" prop="orderId" />
                            <el-table-column label="创建时间" align="center" prop="createTime" width="180">
                                <template #default="scope">
                                    <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
                                </template>
                            </el-table-column>
                            <el-table-column label="备注" align="center" prop="remark" />
                            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                            <template #default="scope">
                                <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['medicine:drug:remove']">删除</el-button>
                            </template>
                            </el-table-column>
                        </el-table>

                        <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
                            v-model:limit="queryParams.pageSize" @pagination="getList" />
                    </div>
                </el-card>
            </el-col>
        </el-row>
    </div>
</template>

<script setup name="Detail">
import { listDetail, getDetail, delDetail, addDetail, updateDetail } from "@/api/his/detail";
import { listSupplier } from "@/api/his/supplier.js";
import { getOrder,updateOrder} from "@/api/his/order.js"
import { useRouter } from "vue-router";
const { proxy } = getCurrentInstance();
const supplier_info = ref([])

const detailList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const route = useRoute();
const router=useRouter();

const orderId = route.params.orderId;
const data = reactive({
    orderForm: {},
    queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderId,
    },
    rules: {
    }
});

const { queryParams, orderForm, rules } = toRefs(data);
//返回
function handleBack(){
    router.push("/medicine/order")
}
//更新采购单状态
function handleUpdateOrderStatus(){
    const data={
        orderId:orderForm.value.orderId,
        orderStatus:0
    }
    proxy.$modal.confirm('确定要重新审核，编号为:"' + orderForm.value.orderNum + '"的采购单吗？').then(function() {
        return updateOrder(data);
    }).then(() => {
        proxy.$modal.msgSuccess("提交审核成功");
    }).catch(() => {});
}
// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.detailId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}
 //删除采购项
 function handleDelete(row){
    const _detailIds = row.detailId || ids.value;
    proxy.$modal.confirm('是否确认删除采购项编号为"' + _detailIds + '"的数据项？').then(function() {
        return delDetail(_detailIds);
    }).then(() => {
        getList();
        proxy.$modal.msgSuccess("删除成功");
    }).catch(() => {});
 }
  //更新采购项的数量
  function updateNum(row){
    const data={
        detailId:row.detailId,
        detailNum:row.detailNum
    }
    updateDetail(data).then(res=>{
        if(res.code==200){
            proxy.$modal.msgSuccess("数量修改成功");
        }
    })
  }
 /** 查找订单 */
 function getOrderInfo(){
    queryParams.value.orderId = orderId
    getOrder(queryParams.value).then(response => {
     orderForm.value = response.data;
     console.log(orderForm.value)
   });
 }
 getOrderInfo();

//异步加载所有的供应商信息
listSupplier().then(res => {
    supplier_info.value = res.rows;
})
/** 查询订单详情信息列表 */
function getList() {
    loading.value = true;
    //queryParams.value.orderId=orderId;
    listDetail(queryParams.value).then(response => {
        detailList.value = response.rows;
        total.value = response.total;
        loading.value = false;
    });
}

// 取消按钮
function cancel() {
    open.value = false;
    reset();
}


/** 修改采购单信息 */
function handleUpdateOrder() {
    updateOrder(orderForm.value).then(res=>{
        proxy.$modal.msgSuccess("修改成功");
        getOrderInfo();
    })
}

/** 重置按钮操作 */
function resetQuery() {
    proxy.resetForm("queryRef");
    handleQuery();
}

/** 导出按钮操作 */
function handleExport() {
    proxy.download('medicine/detail/export', {
        ...queryParams.value
    }, `detail_${new Date().getTime()}.xlsx`)
}

getList();
</script>